require(["common", "jQuery", "coAjax", "queryString", "href", "hash_routie"], function(jSouper, $, coAjax, QueryString, href, hash_routie) {
	hash_routie.on("comments", function() {
		App.set('title', '商品评价');
		var queryString = new QueryString(location.hash);
		var oid = queryString.get("id");
		if (!oid) {
			alert("error", "无效的订单编号");
			href.jump("mobile.personal.html#");
		}
		// 商品详情
		coAjax.get(appConfig.user.ach_by_id_with_eval + oid, function(result) {
			var ach = result.result;
			jSouper.forEach(ach.info.goods_info, function(goods_info) {
				//初始化评价等级为10
				goods_info._eval_star_num = 10
			});
			App.set("user.appraise", ach);
		}, function(errorCode) {
			console.log("errorCode:", errorCode);
			alert("error", "无效的订单编号");
		});
		// 检查评价等级
		App.set('$Event.eval.checkStar', function(e, vm) {
			var star_num = parseInt(this.value);
			if (star_num > 10) {
				vm.set('_eval_star_num', 10);
			} else if (star_num < 0) {
				vm.set('_eval_star_num', 0);
			} else if (isNaN(star_num)) {
				vm.set('_eval_star_num', 10);
			} else {
				vm.set('_eval_star_num', star_num);
			}
		});
		// 提交评价
		App.set("$Event.eval.submitEval", function(e, vm) {
			var evaluationData = [];
			jSouper.forEach(App.get("user.appraise.info.goods_info"), function(goods_info) {
				evaluationData.push({
					goods_id: goods_info.goods._id,
					star_num: ~~goods_info._eval_star_num || 10,
					isAny: !!goods_info._eval_isAny,
					content: goods_info._eval_content
				});
			});
			// console.log(evaluationData);
			var url;
			var state = App.get("user.appraise.info.state");
			if (state == appConfig.ACH_STATE["ol交易完成"]) {
				url = appConfig.user["ach_ol交易完成_to_ol已评价"]
			} else if (state == appConfig.ACH_STATE["交易完成"]) {
				url = appConfig.user["ach_交易完成_to_已评价"]
			} else {
				alert("error", "错误的订单状态")
				return;
			}
			coAjax.put(url + App.get("user.appraise._id"), {
				evaluations: evaluationData
			}, function(result) {
				alert("评价完成");
				console.log(result.result);
				App.set("user.appraise.info.state", result.result);
				hash_routie.emit("comments");
			});
		});
	})
})